﻿//
// Created by work on 24-5-30.
//

#include "CUserInfo.h"

std::map<CUserInfo::EUserCol, QString> CUserInfo::userColToStringMap = {
    {CUserInfo::EUserId, "userid"},
    {CUserInfo::EPwd, "pwd"},
    {CUserInfo::ENickname, "nickname"},
    {CUserInfo::EUtype, "utype"},
    {CUserInfo::EAdmin_usr, "admin_usr"},
    {CUserInfo::EConfigs, "configs"},
    {CUserInfo::EMenu_permission, "menu_permission"},
    {CUserInfo::EProperty_permission, "property_permission"},
    {CUserInfo::EGroupings, "groupings"},
    {CUserInfo::ECreate_time, "create_time"},
    {CUserInfo::EUpdate_time, "update_time"},
    {CUserInfo::ELast_online_time, "last_online_time"},
    {CUserInfo::ELast_login_time, "last_login_time"},
    {CUserInfo::EMail_address, "email_address"},
    {CUserInfo::EPhone_number,"phone_number "}
};

CUserInfo::CUserInfo() {
    utype = EUserType::EUser;
    admin = "root";
    configs = QJsonObject();
    menu_permission = QJsonObject();
    property_permission = QJsonObject();
    grouping = "root";
    create_time = QDateTime::currentDateTime();
    update_time = QDateTime::currentDateTime();
    last_online_time = QDateTime::currentDateTime();
    last_login_time = QDateTime::currentDateTime();
}

QJsonObject CUserInfo::toJson() {
    QJsonObject userJson;
    userJson[userFieldName(USER_ENUM::EUserId)] = userid;
    userJson[userFieldName(USER_ENUM::ENickname)] = nickname;
    userJson[userFieldName(USER_ENUM::EUtype)] = static_cast<int>(utype);
    userJson[userFieldName(USER_ENUM::EAdmin_usr)] = admin;
    userJson[userFieldName(USER_ENUM::EConfigs)] = configs;
    userJson[userFieldName(USER_ENUM::EMenu_permission)] = menu_permission;
    userJson[userFieldName(USER_ENUM::EProperty_permission)] = property_permission;
    userJson[userFieldName(USER_ENUM::EGroupings)] = grouping;
    userJson[userFieldName(USER_ENUM::ECreate_time)] = create_time.toString();
    userJson[userFieldName(USER_ENUM::EUpdate_time)] = update_time.toString();
    userJson[userFieldName(USER_ENUM::ELast_online_time)] = last_online_time.toString();
    userJson[userFieldName(USER_ENUM::ELast_login_time)] = last_login_time.toString();
    userJson[userFieldName(USER_ENUM::EMail_address)] = email_address;
    userJson[userFieldName(USER_ENUM::EPhone_number)] = phone_number;
    return userJson;
}

void CUserInfo::fromJson(const QJsonObject &json)
{
    userid = json[userFieldName(USER_ENUM::EUserId)].toString();
    nickname = json[userFieldName(USER_ENUM::ENickname)].toString();
    admin = json[userFieldName(USER_ENUM::EAdmin_usr)].toString();
    configs = json[userFieldName(USER_ENUM::EConfigs)].toObject();
    utype = EUser;
    menu_permission = json[userFieldName(USER_ENUM::EMenu_permission)].toObject();
    property_permission = json[userFieldName(USER_ENUM::EProperty_permission)].toObject();
    grouping = json[userFieldName(USER_ENUM::EGroupings)].toString();
    create_time = QDateTime::fromString(json[userFieldName(USER_ENUM::ECreate_time)].toString(), Qt::ISODate);
    update_time = QDateTime::fromString(json[userFieldName(USER_ENUM::EUpdate_time)].toString(), Qt::ISODate);
    last_online_time = QDateTime::fromString(json[userFieldName(USER_ENUM::ELast_online_time)].toString(), Qt::ISODate);
    last_login_time = QDateTime::fromString(json[userFieldName(USER_ENUM::ELast_login_time)].toString(), Qt::ISODate);
    email_address = json[userFieldName(USER_ENUM::EMail_address)].toString();
    phone_number = json[userFieldName(USER_ENUM::EPhone_number)].toString();
}

